<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>餐桌列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="=100" align="center"></el-table-column>
      <el-table-column prop="codeId" label="二维码id" header-align="center"></el-table-column>
      <el-table-column prop="num" label="餐桌数量" width="350" align="center"></el-table-column>
      <el-table-column prop="state" label="餐桌状态" width="350" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出的编辑餐桌的对话框 -->
    <el-dialog title="修改店铺" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="二维码id" :label-width="formLabelWidth">
          <el-input v-model="editForm.codeId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="餐桌数量" :label-width="formLabelWidth">
          <el-input v-model="editForm.num" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="餐桌状态" :label-width="formLabelWidth">
          <el-input v-model="editForm.state" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id: '',
        codeId: '',
        num: '',
        state:''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    submitEditForm() {
      let url = 'http://localhost:9989/tables/' + this.editForm.id + '/update'
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = false;
          this.$message({
            message: '修改餐桌信息成功！',
            type: 'success'
          });
          this.loadTableList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadTableList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openEditDialog(table) {
      let url = 'http://localhost:9989/tables/' + table.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadTableList();
            }
          });
        }
      });
    },
    openDeleteConfirm(table) {
      let message = '此操作将永久删除【' + table.name + '】餐桌，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(table);
      }).catch(() => {
      });
    },
    handleDelete(table) {
      let url = 'http://localhost:9989/tables/' + table.id + "/delete";
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除餐桌【' + table.name + '】成功！',
            type: 'success'
          });
          this.loadTableList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadTableList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadTableList() {
      let url = 'http://localhost:9989/tables';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.tableData = responseBody.data;
        }
      });
    }
  },
  mounted() {
    this.loadTableList();
  }
}
</script>